import 'package:flutter/material.dart';

class CustomVerificationCodePainter extends CustomPainter {
  final Map<String?, dynamic>? drawData;

  CustomVerificationCodePainter({required this.drawData});

  final Paint _paint = Paint()
    ..color = Colors.grey
    ..strokeCap = StrokeCap.square
    ..isAntiAlias = true
    ..strokeWidth = 1.0
    ..style = PaintingStyle.fill;

  @override
  void paint(Canvas canvas, Size size) {
    List<dynamic> mList = drawData!["painterData"];

    double offsetX = drawData!['offsetX'];
    // 居中显示画布
    canvas.translate(offsetX, 0);
    // 从map中取值，直接绘制
    for (Map<String?, dynamic>? item in mList) {
      TextPainter painter = item!["painter"];
      double x = item['x'];
      double y = item['y'];
      painter.paint(
        canvas,
        Offset(x, y),
      );
    }

    // 将画布平移回去
    canvas.translate(-offsetX, 0);
    List<dynamic> dotData = drawData!['dotData'];
    for (Map<String?, dynamic> item in dotData) {
      double x = item['x'];
      double y = item['y'];
      double dotWidth = item['dotWidth'];
      Color color = item['color'];
      _paint.color = color;
      canvas.drawOval(Rect.fromLTWH(x, y, dotWidth, dotWidth), _paint);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return this != oldDelegate;
  }
}
